<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" autocomplete="off" class="layui-input" data-search-op="=">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <select class="layui-select" name="category_id" data-search-op="=">
                                    <option value="">- 全部 -</option>
                                    {volist name='category' id='vo'}
                                    <option value="{$key}">{$vo}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select class="layui-select" name="status" data-search-op="=">
                                    <option value="">- 全部 -</option>
                                    {volist name='notes.status' id='vo'}
                                    <option value="{$key}">{$vo}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class=" layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal " lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    let index_url = (location.hash).replace('#', '');
    let edit_url = index_url.replace('/index', '/edit');
    let delete_url = index_url.replace('/index', '/delete');
    let status_json = '{$notes.status|json_encode|raw}';
    let category_json = '{$category|json_encode|raw}';
    layui.use(['form', 'table', 'miniPage', 'element'], function () {
        let $ = layui.jquery, form = layui.form, table = layui.table, miniPage = layui.miniPage;
        form.render()
        table.render({
            elem: '#currentTableId',
            url: index_url,
            method: 'post',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID'},
                {field: 'title', minWidth: 200, title: '标题'},
                {
                    field: 'category_id', minWidth: 100, title: '分类', templet: function (res) {
                        let category = JSON.parse(category_json)
                        return category[res.category_id]
                    }
                },
                {
                    field: 'img', minWidth: 150, title: '图片', templet: function (res) {
                        return res.img ? '<img src="' + res.img + '">' : ''
                    }
                },
                {field: 'article_date', minWidth: 80, title: '文章日期'},
                {
                    field: 'status', minWidth: 80, title: '状态', templet: function (res) {
                        let status = JSON.parse(status_json)
                        return status[res.status]
                    }
                },
                {field: 'c_time', minWidth: 135, title: '创建时间'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let dataField = data.field;
            let formatFilter = {}, formatOp = {};
            $.each(dataField, function (key, val) {
                if (val !== '') {
                    formatFilter[key] = val;
                    let op = $('input[name=' + key + ']').attr('data-search-op');
                    op = op || '%*%';
                    formatOp[key] = op;
                }
            });
            table.reload('currentTableId', {
                page: {curr: 1}, where: {
                    filter: JSON.stringify(formatFilter),
                    op: JSON.stringify(formatOp)
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                let content = miniPage.getHrefContent(edit_url);
                let index = layer.open({
                    title: '新增',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: getArea(),
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId'), data = checkStatus.data;
                let ids = []
                $.each(data, function (key, val) {
                    ids[key] = val.id;
                });
                if (ids.length < 1) return false
                layer.confirm('真的删除么', function (index) {
                    fetchApi(delete_url, {ids: ids}, true, true)
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                let content = miniPage.getHrefContent(edit_url + '?id=' + data.id);
                let index = layer.open({
                    title: '编辑',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: getArea(),
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    fetchApi(delete_url, obj.data)
                    layer.close(index);
                });
            }
        });

    });
</script>